import SceneManager from "./Scene/Manager";
import DialogManager from "./Dialog/Manager";

class Loader {
    constructor () {
        this.sceneLoadPage = null;
        this.dialogLoadPage = null;
        this.sceneClassMap = {};
    }

    setLoadPageForScene (page) {
        if(page && page instanceof Laya.Node && !page.destroyed){
            page.zOrder = 10;
            this.sceneLoadPage = page;
        }
    }

    setLoadPageForDialog (page) {
        if(page && page instanceof Laya.Node && !page.destroyed){
            this.dialogLoadPage = page;
        }
    }

    getLoadingPage (type) {
        let loadingPage = null;
        switch (type) {
            case "Scene":
                loadingPage = this.sceneLoadPage;
                break;
            case "Dialog":
                loadingPage = this.dialogLoadPage;
                break;
        }

        return loadingPage;
    }

    showLoadingPage (type) {
        const loadingPage = this.getLoadingPage(type);
        if(!loadingPage){return;}

        loadingPage.onOpened && loadingPage.onOpened();
        switch (type) {
            case "Scene":
                SceneManager.addChild(loadingPage);
                break;
            case "Dialog":
                DialogManager.addChild(loadingPage);
                break;
        }
    }

    hideLoadingPage (type) {
        const loadingPage = this.getLoadingPage(type);
        
        if(loadingPage){
            loadingPage.removeSelf();
        }
    }

    onLoadProgress (type, value) {
        let loadingPage = null;
        switch (type) {
            case "Scene":
                loadingPage = this.sceneLoadPage;
                break;
            case "Dialog":
                loadingPage = this.dialogLoadPage;
                break;
        }
        if (loadingPage) {
            loadingPage.onProgress && loadingPage.onProgress(value);
        }
    }

    onLoadComplete (url, loader, type, complete) {
        loader.off(/*laya.events.Event.PROGRESS*/"progress", null, this.onLoadProgress);
        var obj = Laya.Loader.getRes(url);
        var scene = null;

        if (!obj) {throw `Can not find "${type}":${url}`;}
        if (!obj.props) {throw `"${type}" data is error:${url}`;}
        
        var runtime = obj.props.runtime ? obj.props.runtime : obj.type;
        var clas = Laya.ClassUtils.getClass(runtime);
        this.sceneClassMap[url] = clas;

        if (obj.props.renderType == "instance"){
            scene = clas.instance || (clas.instance = new clas());
        }else {
            scene = new clas();
        }
        if (scene && (scene instanceof laya.display.Node )){
            scene.url = url;
            if (!scene._getBit(/*laya.Const.NOT_READY*/0x08)){
                complete && complete.runWith(scene);
            }else {
                scene.on("onViewCreated", null, function(){
                    complete && complete.runWith(scene)
                })
                scene.createView(obj);
            }
            this.hideLoadingPage(type);
        }else {
            throw `Can not find "${type}":${runtime}`;
        }
    }

    load (type, url, complete) {
        if(this.sceneClassMap[url]){
            return complete.runWith(new this.sceneClassMap[url]);
        }
        this.showLoadingPage(type);
        Laya.loader.resetProgress();
		var loader = new Laya.SceneLoader();
		loader.on(/*laya.events.Event.PROGRESS*/"progress", this, this.onLoadProgress, [type]);
		loader.once(/*laya.events.Event.COMPLETE*/"complete", this, this.onLoadComplete, [url, loader, type, complete]);
		loader.load(url);
    }
}

export default new Loader;